% Endogenous Firm Competition and Cyclicality of Markups
% Hassan Afrouzi
% November 2015


%----------------------------------------------------------------
% 1. Defining variables
%----------------------------------------------------------------

var rho Drhot Gamma;                    %rho is markup of a cheating firm relative to industry's collusion price
                                        %Drhot is the demand of cheating firm relative to its demand if it commited to collusion
                                        %Gamma is the gains from collusion relative to punishment, normalized by today's demand.
var y c k x l lambda nu r w mu;         %y is output, c is consumption, k is capital, x is investment, l is labor,
                                        %lambda is marginal utility of consumption, nu over lambda is price of investment
                                        %r is rate of return on capital, w is wages and mu is the average markup in the economy.
var q S Sp g dy z_a z_g;                %q is the stochastic discount factor, S is the function in the investment adjustment cost,
                                        %Sp is derivative of S, g is government spending, z_a is technology shock and z_g is the 
                                        %government spending shock
varexo e u;
 
parameters beta psi delta alpha gamma gss sigma theta phi N eta muc;
parameters a b rho_a rho_g_1 rho_g_2;
parameters var_e var_u;                 %parameters are explained below.

%----------------------------------------------------------------
% 2. Calibration
%----------------------------------------------------------------
load parameterfile;
set_param_value('a',a_p);               %Investment adjustment cost parameters
set_param_value('b',b_p);               %Habit persistence parameter
set_param_value('rho_g_1',rho_g_1_p);   %first lag coefficient in gov't spending shock
set_param_value('rho_g_2',rho_g_2_p);   %second lag coefficient in gov't spending shock
set_param_value('gamma',gamma_p);       %1-probability of renegotiation
set_param_value('N',N_p);               %Number of firms in each industry
set_param_value('var_e',var_e_p);       %variance of technology shock
set_param_value('var_u',var_u_p);       %variance of gov't spending shock
set_param_value('phi',phi_p);           %coefficient on disutility of labor
theta	= 2;                            %inverse of intertemporal elasticity of substitution
alpha   = 0.35;                         %share of capital in output
beta    = 0.993;                        %HH's discount factor
delta   = 0.025;                        %depreciation of capital
set_param_value('psi',psi_p);           %inverse Frisch elasticity of labor
rho_a   = 0.95;                         %persistence of technology shock
set_param_value('sigma',sigma_p);       %elasticity of substitution across industry goods
set_param_value('eta',eta_p);           %elasticity of substitution within industry goods
gss     = -1.6;                         %log(steady state government spending as share of output)
muc     = (eta*(N-1)+sigma)/(eta*(N-1)+sigma-N);    %Markup when everyone plays the static Nash equilibrium (punishment)


%----------------------------------------------------------------
% 3. Model: 
%    all variables are defined as logs of the corresponding 
%    variables in the model, except for function S(.) and 
%    its derivative, Sp.
%----------------------------------------------------------------

model; 

%--------------------
% 3.1. Definitions
%--------------------

% Defining cost of investment
    
    S=(a/2)*((exp(x-x(-1))-1)^2);

% Derivative of function S(.)

    Sp=a*(exp(x-x(-1))-1);

% Expected Growth rate of output

    dy=y(+1)-y;

% Stochastic Discount Factor

    q=log(beta)+lambda(+1)-lambda;

% Demand for a cheating firm

    exp(Drhot)=(exp(rho)^-eta)*((exp(rho)^(1-eta)+N-1)/N)^((eta-sigma)/(1-eta));


%--------------------
% 3.2. HHs
%--------------------

% FOC with respect to consumption
    
    exp(lambda)=(exp(c)-b*exp(c(-1)))^(-theta)-b*beta*(exp(c(+1))-b*exp(c))^(-theta);

% FOC with respect to labor

    phi*exp(l)^psi=exp(lambda)*exp(w);

% FOC with respect to investment
    
    exp(lambda)=exp(nu)*(1-S-(exp(x-x(-1)))*Sp)+beta*exp(nu(+1))*((exp(x(+1)-x))^2)*Sp(+1);

% FOC with respect to capital

    exp(nu)=beta*(exp(lambda(+1))*exp(r(+1))+exp(nu(+1))*(1-delta));

% Law of motion for capital

    (1-S)*exp(x)=exp(k)-exp(k(-1))*(1-delta);



%--------------------
% 3.3. Firms
%--------------------

% Production function
    
    y=z_a+alpha*k(-1)+(1-alpha)*l;

% Cost minimization FOC for capital

    r=-mu+log(alpha)+y-k(-1);

% Cost minimization FOC for labor

    w=-mu+log(1-alpha)+y-l;

% First order condition for relative cheating markup

    exp(rho)*exp(mu)/(exp(rho)*exp(mu)-1)=eta-(eta-sigma)/((N-1)*exp(rho)^(eta-1)+1);

% Law of motion for markups

(exp(rho)-exp(-mu))*exp(Drhot)-(1-exp(-mu))=gamma*exp(q)*(exp(q+dy+Gamma(+1)));
exp(Gamma)=(1-exp(-mu))-(1-1/muc)*(muc/exp(mu))^(1-sigma)+gamma*(exp(q+dy+Gamma(+1)));


%--------------------
% 3.4. Mkt Clearing & Gov't
%--------------------

exp(c)+exp(x)+exp(g)=exp(y);
g=gss+z_g;

%--------------------
% 3.5. Shock Processes
%--------------------

% Technology Shock

    z_a=rho_a*z_a(-1)+e;

% Gov't Spending Shock

    z_g=rho_g_1*z_g(-1)+rho_g_2*z_g(-2)+u;



end;

%----------------------------------------------------------------
% 4. Computation
%----------------------------------------------------------------

initval;
y       = log(1.01);
c       = log(0.56);
k       = log(8.4);
x       = log(0.2);
l       = log(0.33);
S       = 0;
Sp      = 0;
mu      = 0.3;
q       = 0;
y       = 0;
rho     = -.15;%-.06;
Drhot   = 0.06;%0.57;
Gamma   = -2.78;
r       = log(0.03);
w       = log(1.63);
q       = log(beta);
z_a     = 0;
z_g     = 0;

end;

shocks;

var e = var_e;
var u = var_u;

end;

steady;

check;

stoch_simul(periods=10000,drop=1000,order = 1,noprint,nograph,irf=20);